/**
 * @author zqd	差旅小组
 *
 * 下午04:18:51
 */
package com.cbts.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter{

	/* 
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		// zqd 下午04:18:52

	}

	/* 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain fc) throws IOException, ServletException {
		// zqd 下午04:18:52
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;
		
		String url=request.getRequestURL().toString();
		String contextPath=request.getContextPath();
		String userid=null;
		
		if ((url.indexOf("userInfoAction!login.action")!=-1)
				||(url.indexOf("userInfoAction!logout.action")!=-1)
				||(url.indexOf("/unlogin.jsp")!=-1)
				||(url.indexOf("login.jsp")!=-1)
				||(url.indexOf("/index.jsp")!=-1)
				||(url.compareTo("http://localhost:8080/cbts/")==0
				||url.indexOf("forgetPWD.action")!=-1
				||url.indexOf("validateEmail.action")!=-1
				||url.indexOf("/gif.jsp")!=-1
				)){
			//if(userid!=null){
				//response.sendRedirect(contextPath + "/showchart!prepareSetting.action");
				//return ;
			//}
			//else{
				fc.doFilter(req, res);
				return ;
			//}
		}else{
			if(!request.isRequestedSessionIdValid()){
				response.sendRedirect(contextPath + "/unlogin.jsp");
				return ;
			}else{
				userid=(String)request.getSession().getAttribute("userid");
				if(userid==null){
					response.sendRedirect(contextPath + "/unlogin.jsp");
					return ;
				}else{
					fc.doFilter(req, res);
					return ;
				}
			}
		}
	}

	/* 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig arg0) throws ServletException {
		// zqd 下午04:18:52

	}
}
